Gradually Implementing CSS in Your Site
Greg Rewis
Dreamweaver Evangelist
In my last article, Why Use CSS?, I talked about the benefits of using CSS in your web design and development endeavors. Since that article hit the Macromedia website, I have been literally inundated with e-mail asking, How do I move from traditional HTML markup to CSS? The answer, as you all know, is "Very carefully."
All kidding aside, the truth of the matter is that there is no silver bullet
capable of slaying the <table> and <font> dragons
and rendering your site a complete, fluid, and flexible CSS masterpiece. However,
as any good late-night infomercial will tell you, a nip here and a tuck there
will have your site looking like new, and even easier to maintain.
In other words, contrary to popular opinion, CSS is not an all-or-nothing decision. You can gradually implement CSS into your designs and, in turn, gradually reap the benefits upon which I expounded in my previous article.
The first step in implementing CSS (after repeating the requisite "I
will no longer use the <font> tag" mantra), is to identify
exactly "what" is wrong with the current design. While HTML is relatively
easy to learn, it also is extremely limited in its design capabilities. The
reasons for the limitations are simply that the language was designed to "mark
up" or identify content—not create complex designs on par with traditional
page layout applications such as QuarkXPress or Adobe InDesign. Of course,
intrepid designers found out very quickly that the tags which were designed
to identify content could also be leveraged as a design tool. In doing so,
however, they began to leave behind the true advantages of a markup language—the
ability to change the presentation of the markup easily.
If you've ever inherited a site from a client and sat silently pondering the
designer's repeated use of the <font> tag or infinitely
nested tables, which even an MIT graduate would find difficult to decipher,
you already know what I'm talking about. For the rest of you, the presentational
markup simply ends up getting in the way of maintaining and updating the site.
Imagine the difficulty of modifying a layout containing 1500 individual font
tags in a single page! (If you think I'm exaggerating, I'll refer you to a
recent seminar attendee I met in Miami named Chris, who told me about this
very "slight modification" to a site.)
The core essence of CSS is to replace the presentational markup in HTML documents. But that statement begs the question: What is presentational markup? You can boil the answer down to any tag or attribute which determines the way a browsing device displays a piece of content.
As you begin to identify tags and attributes to remove, it is important to remember that removing certain tags and replacing them with CSS might cause the page to degrade beyond an acceptable level in older browsers. Only you can draw the line about what your site's target browser level should be. For the purposes of this article, you'll remove only the styling information from the page, leaving the underlying table structure. This means that the page will still render its basic layout in older browsers but give you added flexibility through CSS to style the content—and you'll be a long way down the road, should you decide to replace the table structure with a pure CSS design at a later date.
Before proceeding, it is important to mention this final warning: Make sure you back up the page(s) that you intend to modify before proceeding.
Requirements
To complete this tutorial you will need to install the following software:
Macromedia Dreamweaver MX 2004
Replacing <body> Attributes
Now it's time to start styling. For the rest of this article, you'll look at tags to replace and create styles to mimic, and in most cases even improve their presentation. To follow along in Dreamweaver MX 2004, simply open any of your table-based, traditionally styled documents. Back up this page before proceeding.
Your first step in implementing CSS is to replace the traditional page properties
defined as attributes of the <body> tag (your <body> tag
attributes will appear slightly different). Suppose your <body> tag
looks like the following:
<body bgcolor="#E6e6e6" text="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
Follow these steps to replace the <body> tag attributes:
- Start Dreamweaver MX 2004.
- Open an HTML document with a table in it.
- Switch to Code view.
- Locate the
<body>tag, select all the attributes following the word body, and delete them.
Caution: If you have any JavaScript in your <body> tag,
such as an OnLoad handler, leave those attributes as is. In the
above example, you no longer need any of the attributes such as bgcolor, text, leftmargin,
and so forth. You will replace all those elements by redefining the <body> tag
through CSS.
To redefine the <body> tag in Dreamweaver MX 2004, you
have two options. You can use the Page Properties command or define the style
manually using the CSS Styles panel. In this example, you'll use the Page Properties
dialog box to define the style. This has the disadvantage of adding the style
to the document itself instead of adding it to an external style sheet. While
you'll eventually want to put the styles in an external style sheet, I find
it easiest to work locally and then export the styles when I know they are
properly defined.
Follow these steps to redefine the <body> tag:
-
Select Modify > Page Properties to set all of the attributes using the values above, for
bgcolor,text, and so forth. Note that in Dreamweaver MX 2004 you can define a default font and font size, as well as a number of other attributes that were not available when you relied upon standard HTML to design the appearance of your page. For now, however, just modify the attributes that you deleted above. Dreamweaver creates a new style definition in the<head>section of your document with two rules:<style type="text/css"> <!-- body,td,th { color: #FFFFFF; } body { background-color: #e6e6e6; margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; } --> </style> - To see the style definition, open the CSS Styles Panel (select Window > CSS
Styles) and expand the
<style>tag. - Notice that the
bodytag appears twice in the code above. The first one is a line withbody,td, andth. For those of you who are new to CSS, this means that you have redefined all three tags at once, giving all of them the text color #FFFFFF.
Remember that one of the reasons for implementing CSS is to reduce the amount of code for rendering a page. CSS combines the definition of multiple rules by putting them in a single line and separating the individual entries with a comma.
Saying Goodbye to the <font> Tag
Now you need to remove the deprecated <font> tags from
your document. Before you remove them, however, you should investigate a bit.
Most likely, the majority of your document uses a single font set. In fact,
most <font> tags in the document probably use the same basic
definition. Figure out what that is, but for the purposes of this example,
assume that there are four basic uses of the <font> tag
in the page:
<font face="Arial, Helvetica, sans-serif" size="3"> <font face="Arial, Helvetica, sans-serif" color="#990000" size="+1"> <font face="Arial, Helvetica, sans-serif" color="#FFFFFF" size="-2"> <font face="Times New Roman, Times, serif" size="4">
As you can see, most of the <font> tags use Arial as their
font. For this tutorial, assume that the first instance is the most common.
This means that it is the candidate that becomes your default text style:
- Remove the
<font>tags from the document by selecting Edit > Find and Replace. - In the Find and Replace dialog box, select Specific Tag in the Search pop-up
menu and choose Font from the subsequent pop-up menu to the right. Because
you are looking for all
<font>tags in the document, regardless of their attributes, click the minus (–) button to remove the attribute criteria. - From the Action pop-up menu, choose Strip Tag and then click the Replace All button.
- With all of the
<font>tags gone, create a global rule for the text on the page by modifying thebody,td,thrule that Dreamweaver has already created. Simply open the Page Properties dialog box again. From the Page font list, select the appropriate font set.
At this point, let's discuss font sizes. There are two units that you must consider: pixels and em spaces. To determine which unit is right for you, consider which criterion is most important: visual design or accessibility. If you want to ensure that your page renders the same across the various browsers and platforms, use pixels as your unit of measure. In doing so, however, you render your site less accessible. This is because a pixel is a fixed unit of measurement and causes older browsers and screen assistants to increase the size of the text. By using em spaces, you establish a flexible unit of measurement that lets users increase the size of the text in their browsers. However, this means that your design may then reflow. Whichever unit you choose is simply a matter of knowing your target audience. For this example, use the em space.
An em space is simply equal to the width of an m in the selected font. In other words, if a Microsoft Internet Explorer is set to its standard font display (approximately 14 pixels), then one em equals 14 pixels.
To set the font size, select Modify > Page Properties. Set the font size
to 1 ems because the <font> tag used a size of 3, which
is equal to the browser's default size. This produces a rule as follows:
body,td,th {
color: #FFFFFF;
font-size: 1em;
font-family: Arial, Helvetica, sans-serif;
}
Dreamweaver simply adds the attributes to the previous definition of the body, td,
and th tags, instead of adding an entirely new definition. Looking
at your page in Design view, notice that the text on the page appears using
the definition from the rule.
Note: The unit of measure is actually "em" and Dreamweaver does write it properly in the code. In the pop-up menu, however, it appears—incorrectly, in my opinion—as "ems."
With your first <font> tag now replaced by a CSS rule,
you can create rules to replace the other instances of the <font> tags,
which you already removed from the document.
To create the next rule:
-
Return to the CSS Styles panel and click the New CSS Style button (at the bottom of the CSS Styles panel). Apply this new rule individually to appropriate elements on the page. Because you want to be able to use the style many times on the page, define this as a Class, select this document only, and name it .title. Notice that the name includes a period ".", which indicates that it is a class definition. In CSS, a period precedes a class name, a tag definition contains only the name of the tag, and a pound or hash (#) sign precedes an ID name. Remember that the tag looked like this:
<font face="Arial, Helvetica, sans-serif" color="#990000" size="+1">
- The CSS Style Definition for .title dialog box appears. Because this tag
uses the same font as the redefined
body,td, andthdefinition, you only need to change the color and size. Select #990000, a dark red for the text category. - To make the text slightly larger than the standard font size, set the size to 1.2 em, which will make it 20 percent larger than normal.
- To apply the style, simply select the text on the page—or, if the text is by itself in a table cell, select the table cell. Using the Property inspector, select the title style (the style you just created) from the Style pop-up menu.
Notice that the text continues to appear in Arial because that element inherits
that particular attribute from the parent container, in this case the body, td,
or th tag.
Following these steps, you can create and apply rules to replace the rest
of the <font> tag in your document.
Fixing Other Undesirable Attributes
Use the Dreamweaver Find and Replace command to remove all the other extraneous attributes from your layout, replacing them with rules instead. To achieve the maximum benefit of CSS in your designs, remove the following tags:
<br>tagsbgcolorandbackgroundattributesalignandvalignattributes- table attributes such as
borderandcellpadding
Optionally, you can remove the width attribute from <table> tags,
as well as the height attribute inside <td> tags.
However, you can also leave any or all of these attributes if you want to ensure
that your layout still renders well in older browsers. Adding attributes to
your CSS rules to replace these attributes is a straightforward process; most
attributes have a similar name because you can find them in the CSS Styles
dialog box.
Once you complete the styling of your document, you have one final step to achieve your goal. You need to move all the styles out of your document and place them in an external style sheet. Doing this lets you use the styles in other documents by linking to the style sheet.
Use the following steps to export styles:
- Return to the CSS Styles panel and right-click (Control-click on Mac) the style name.
- In the context menu, select Export.
- An Export Styles dialog box opens, asking you to specify a name for the new style sheet. Name the style sheet main.css and click OK.
- Although Dreamweaver exports the styles, they are still in the document.
To remove them from the document, select the
<style>tag in the CSS Styles panel and click the Trash icon in the lower right corner of the CSS Styles panel. - Click the Attach Style Sheet icon at the bottom of the CSS Styles panel and browse to your new external style sheet, main.css.
Voilà! You have now styled a page with CSS!
Now if you want to modify any of the styles, simply place your cursor in an element with the applied style and use the new Relevant CSS panel to modify the rule. You can find this panel by expanding the Tag Inspector panel and clicking the Relevant CSS tab. I like to work with this panel displaying a complete list of the attributes, so I click the Show List View button instead of the Show Category View button. This lists all the available CSS attributes for the selected rule.
Additionally, the top of the Relevant CSS panel displays the full cascade of styles applied all the way down to the selected style. At the top of the list, in blue, are the attributes which have a value in the selected rule. To add an attribute, simply locate it in the list and click the field to enter its value. Dreamweaver is smart enough to know that the style is actually in an external style sheet, so it opens the style sheet and modifies it. Because the style sheet is external to your document, Dreamweaver applies your modifications not only to the current document, but to all other documents in your site that link to it.
Next Steps
You've begun the journey toward a CSS-based website. I hope this article whetted your appetite for using CSS. Obviously, there is much more to learn. I encourage you to read the other great articles about CSS here on macromedia.com:
- An Overview of CSS in Dreamweaver MX 2004
- Designing with CSS in Dreamweaver MX 2004
- From Screen to Print: Creating a Print CSS
- Styled Headings for Authors and Users
Until next time, happy styling!